import java.util.*;

public class task1 {

	
	public static void instruction(){

		System.out.println("This program computes the approximate value of the factorial using the formula proposed by R. W. Gosper.");
		System.out.println("To use this program, the input should be non-negative. Also use values not greater than 143");
	}

	public static double factorial(int num)throws IllegalArgumentException{
		
		if(num < 0 || num > 143)
		throw new IllegalArgumentException("Error: java.lang.IllegalArgumentException");
		else{
		double y;

		y = Math.pow(num,num)*(1/Math.pow(Math.E,num))*Math.sqrt(Math.PI * (2*num+1/3));
		
		return y;
		}
	}

	public static void main(String[] args) {

	instruction();
	
	Scanner sc = new Scanner(System.in);
	
	try{

		int num;

		System.out.print("Enter the value of n [0 <= n <= 143]: ");
		num = sc.nextInt();
		
		double f;
		
		if(num == 0)
			f = 1;
		else
			f = factorial(num);
		
		System.out.printf("The approximate factorial of %d is %.6e",num,f);
		
		}catch(IllegalArgumentException e){
			System.out.println(e);
		}catch(InputMismatchException e){
			System.out.println("java.util.InputMismatchException");
		}

	sc.close();

	}
	}
